package com.taru.houseapp.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.houseapp.commen.JsonResult;
import com.taru.houseapp.entity.*;
import com.taru.houseapp.service.IDynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class DynamicController {

    @Autowired
    IDynamicService dynamicService;
    /**
     * 查询所有动态圈
     */
    @RequestMapping(value = "/dynamic/all",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult queryDynamic(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        JsonResult result = null;
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Dynamic> dynamics = dynamicService.queryDynamic();
            PageInfo pageInfo=new PageInfo(dynamics);
            result = new JsonResult(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 发布动态圈
     */
    @RequestMapping(value = "/dynamic",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDynamic(@RequestBody Dynamic dynamic){
        JsonResult result = null;
        if(dynamic.getUserId()==null || dynamic.getUserId().length()<=0||dynamic.getContext()==null || dynamic.getContext().length()<=0 || dynamic.getImageList()==null){
            result = new JsonResult(200, "参数错误");
            return result;
        }
        try {
            dynamicService.addDynamic(dynamic);
            result = new JsonResult(200, "发布动态成功");
            } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 删除动态
     */
    @RequestMapping(value = "/dynamic",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult decletedDynamic(@RequestBody List<Integer> ids){
        JsonResult result = null;
        try {
            dynamicService.decletedDynamic(ids);
            result = new JsonResult(200, "删除动态成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 查询我发布的动态圈
     */
    @RequestMapping(value = "/dynamic/myself",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult queryMyDynamic(String userId,
                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        JsonResult result = null;
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Dynamic> dynamic = dynamicService.queryMyDynamic(userId);
            PageInfo pageInfo=new PageInfo(dynamic);
            result = new JsonResult(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 查询动态圈评论
     */
    @RequestMapping(value = "/dynamic/comment/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult queryDynaComment(@PathVariable("id") String id) {
        JsonResult result = null;
        try {

            List<DynaComment> dynaComments = dynamicService.queryDynaComment(id);
            result = new JsonResult(200, "查询成功", dynaComments);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 查询给动态点赞的人
     */
    @RequestMapping(value = "/wholikedynamic",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult whoLikeDynamic(String id) {
        JsonResult result = null;
        try {
            List<User> users = dynamicService.whoLikeDynamic(id);
            result = new JsonResult(200, "查询成功", users);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }

    /**
     * 删除动态评论
     */
    @RequestMapping(value = "/dynamic/comment",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult decletedDynaCom(@RequestBody List<Integer> ids){
        JsonResult result = null;
        try {
            dynamicService.decletedDynaCom(ids);
            result = new JsonResult(200, "评论删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 添加动态评论
     */
    @RequestMapping(value = "/dynamic/comment",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDynamicCom(@RequestBody DynaComment dynaComment){
        JsonResult result = null;
        try {
            dynamicService.addDynamicCom(dynaComment);
            result = new JsonResult(200, "评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 点赞动态
     */
    @RequestMapping(value = "/dynamic/tolike",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult likeDynamic(@RequestBody VoDynaLike voDynaLike){
        JsonResult result = null;
        try {
            dynamicService.likeDynamic(voDynaLike);
            result = new JsonResult(200, "点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 取消点赞动态
     */
    @RequestMapping(value = "/dynamic/tolike",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult noLikeDynamic(@RequestBody VoDynaLike voDynaLike){
        JsonResult result = null;
        try {
            dynamicService.noLikeDynamic(voDynaLike);
            result = new JsonResult(200, "取消点赞成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
    /**
     * 判断是否点赞该动态
     */
    @RequestMapping(value = "/dynamic/islike",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult whetherLikeDynamic(@RequestBody VoDynaLike voDynaLike){
        JsonResult result = null;
        try {
            VoDynaLike voDynaLike1=dynamicService.whetherLikeDynamic(voDynaLike);
            if (voDynaLike1 == null) {
                result = new JsonResult(200, "查询成功",false);
            } else {
                result = new JsonResult(200, "查询成功", true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }

    /**
     * 谁赞我
     * @param userId
     * @return
     */
    @RequestMapping(value = "/dynamic/collection/me",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult queryUserByCollectionMe(String userId,
                                              @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize",required = true,defaultValue = "10") Integer pageSize){
        JsonResult result = null;
        if(userId ==null || userId.length()<=0){
            result = new JsonResult(404, "参数错误");
            return result;
        }
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<VoDynaColl> list=dynamicService.queryUserByCollectionMe(userId);
            PageInfo pageInfo=new PageInfo(list);
            result=new JsonResult(200,"获取成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }

    /**
     * 我赞过谁
     * @param userId
     * @return
     */
    @RequestMapping(value = "/dynamic/collection/who",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult queryUserByWhoLikeMe(String userId,
                                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize",required = true,defaultValue = "10") Integer pageSize){
        JsonResult result = null;
        if(userId ==null || userId.length()<=0){
            result = new JsonResult(404, "参数错误");
            return result;
        }
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<VoDynaColl> list=dynamicService.queryUserByWhoLikeMe(userId);
            PageInfo pageInfo=new PageInfo(list);
            result=new JsonResult(200,"获取成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(500, "服务器内部错误");
        }
        return result;
    }
}
